<?php

require('pagina.php');
require('database.php');
require('auth.php');
require('core.php');

// Verifica se esta autenticado com pelo menos nivel 3
validaPermissao(3);

// Inicia cabecalho da pagina
pagina_inicio('ParkSys - Locations');
adicionar_js('toggle.js');
adicionar_css('locations.css');
adicionar_css('datechooser.css');
adicionar_js('datechooser.js');
pagina_head();

// Mostra opcoes
echo "<ul id=opcoes>";
echo "<li class='positive'><a href='location_new.php'><img src='img/add.png' alt=''>New Location</a></li>";
echo "<li><a href='locations.php?&amp;print=1",
	(isset($_GET['from'])? "&amp;from={$_GET['from']}" : ""),
	(isset($_GET['to'])? "&amp;to={$_GET['to']}" : ""),
    "' target='_blank'><img src='img/print.png' alt=''>Print</a></li>";
echo "<li><a href='locations_table.php?",
	(isset($_GET['from'])? "&amp;from={$_GET['from']}" : ""),
	(isset($_GET['to'])? "&amp;to={$_GET['to']}" : ""),
    "' target='_blank'><img src='img/excel.png' alt=''>Excel Table</a></li>";
echo "</ul><br><br>";

// Formulario para entrar com o periodo de tempo
echo "<form id=periodo method=GET action=locations.php>";
echo "<label for=from>From:</label> <input id=from type=TEXT value='" . @$_GET['from'] . "' name='from' size=9>",
	"<img class=imgcalendar src='img/calendar.gif' onclick=\"showChooser(this, 'from', 'chooserSpan1', 2000, 2020, Date.patterns.ShortDatePattern, false);\" alt='calendar'>",
	"<div id='chooserSpan1' class='dateChooser select-free' style='display: none; visibility: hidden; width: 160px;'></div>";
echo "<label for=to>To:</label> <input id=to type=TEXT value='" . @$_GET['to'] . "' name='to' size=9>",
	"<img class=imgcalendar src='img/calendar.gif' onclick=\"showChooser(this, 'to', 'chooserSpan2', 2000, 2020, Date.patterns.ShortDatePattern, false);\" alt='calendar'>",
	"<div id='chooserSpan2' class='dateChooser select-free' style='display: none; visibility: hidden; width: 160px;'></div><br>";
echo "<label for=search>Search:</label> <input type=TEXT name=search id=search value='' maxlength=50 size=10> ";
echo "<input type=SUBMIT value='submit' name='ok'>";
echo "</form>";

/* Show locations table */
flush();
echo "<div id=table_container>";
include('locations_table.php');
echo "</div>";

if (!isset($_GET['print']))
{
	$resultado = query_bd("SELECT * FROM locations WHERE operating=FALSE");

	// Mostra uma tabela com todas as locacoes, e alguns dados
	echo "<em><a href=\"javascript:toggle('non_operating')\" >Non operating locations(", mysql_affected_rows(), ")</a></em>";
	echo "<table id=non_operating style='display: none'>";
	echo "<tr><th>Name<th>Working time<th>Address<th>Manager contact<th>Manager payrate<th>Valet payrate<th>Insurance tax";

	$mark = False;
	while ($location = mysql_fetch_array($resultado))
	{
		$location_contact = query_fetch("SELECT * FROM contacts WHERE id={$location['locationcontact']}");
		$manager_contact = query_fetch("SELECT * FROM contacts WHERE id={$location['managercontact']}");

		echo "<tr ", ($mark = !$mark? "class=mark" : ""), ">";

		echo "<td><a href='location.php?id={$location['id']}'>{$location_contact['name']}</a>";
		echo "<td>{$location['workingtime']}";
		echo "<td>{$location_contact['number']} {$location_contact['street']} {$location_contact['complement']}<br>",
			"{$location_contact['city']} {$location_contact['state']} {$location_contact['zipcode']}<br>",
			"{$location_contact['phone']} {$location_contact['cellphone']}<br>{$location_contact['email']}";
		echo "<td>{$manager_contact['name']}<br>".
			"{$manager_contact['number']} {$manager_contact['street']} {$manager_contact['complement']}<br>",
			"{$manager_contact['city']} {$manager_contact['state']} {$manager_contact['zipcode']}<br>",
			"{$manager_contact['phone']} {$manager_contact['cellphone']}<br>{$manager_contact['email']}";
		echo "<td>&#36;{$location['manager_payrate']}";
		echo "<td>&#36;{$location['valet_payrate']}";
		echo "<td>&#36;{$location['insurance_tax']}";
		unset($location);
	}
	unset($resultado);

	echo "</table>";
}

// Finaliza pagina
pagina_fim();
?>
